import React from 'react';
import { connect } from 'dva';
import { createForm } from 'rc-form';
import styles from './index.less';
import { Toast, Card, Button, Modal, WingBlank, WhiteSpace } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import Block from 'fs-flex'
import Header from '../../../../components/Header/index'
import WindowService from '../../../../services/windowService'
const alert = Modal.alert
class WindowPage extends React.Component {
    state = {
        show: false,
        data: []
    }
    async componentDidMount() {
        const { code, data, msg } = await WindowService.windowInfo()
        this.setState({ data: data })
    }
    edit(data, index) {
        this.props.history.push({ pathname: '/manage/window/edit', state: { data: data } })
    }
    create() {
        this.props.history.push({ pathname: '/manage/window/edit' })
    }
    async delete(delData) {
        console.log(delData)
        const { code, msg } = await WindowService.deleteWindow(delData)
        if (code === "0000") {
            const { data, msg } = await WindowService.windowInfo()
            this.setState({ data: data })
            Toast.info("删除成功！", 4)
        } else {
            Toast.info(msg, 3)
        }

    }
    render() {
        return (
            <Block vf>
                <Block>
                    <Header needBack='true' title='窗口管理' history={this.props.history} />
                </Block>
                <WingBlank size="lg">
                    <WhiteSpace size="lg" />
                    {this.state.data && this.state.data.length > 0 ? this.state.data.map((data, idx) => (
                        <Card key={idx}>
                            <Card.Header title={data.windowName} />
                            <Card.Body>
                                <Block>
                                    <Block>账号：{data.account}</Block>
                                    <Block mt={5}>可用状态：{data.status ? "可用" : "停用"}</Block>
                                    <Block wf mt={5}>
                                        <Block>
                                            <Button type="primary" size="small" onClick={this.edit.bind(this, data, idx)} inline>编辑</Button>
                                        </Block>
                                        <Block ml={5}>
                                            <Button type="primary" size="small" onClick={() => {
                                                alert('删除确认', '确认删除该记录么？', [
                                                    { text: '取消', onPress: () => console.log('cancel') },
                                                    { text: '确认', onPress: () => this.delete(data) },
                                                ])

                                            }} inline>删除</Button>
                                        </Block>
                                    </Block>
                                </Block>
                            </Card.Body>
                        </Card>
                    )) : null}
                    <WhiteSpace size="lg" />
                    <Block wf mt={15}>
                        <Block f='3'></Block>
                        <Block f='4'><Button a='c' f='c' size='small' type="primary" onClick={() => {
                            this.create()
                        }}
                        >新建窗口账号</Button></Block>
                        <Block f='3'></Block>
                    </Block>
                </WingBlank>
            </Block>
        );
    }

}

WindowPage
    .propTypes = {
    };

export default createForm()(connect()(WindowPage));
